<?php
class cls_misc{
	public function toString($str,$category){
		include_once 'db_conn.php';
		$sql_str="select * from codes where code='{$str}' and category='{$category}'";
		$sql_qry=mysql_query($sql_str) or die(mysql_error());
		$row=mysql_fetch_array($sql_qry);
		$value=strtoupper($row['descr']);
		return $value;
		mysql_freeresult();
	}

	public function sanitize_hyp($str){ //convert hyphen values to underscore values
		$ireplace_value="_";
		$search_value="-";
		$value=str_replace($search_value,$ireplace_value,$str);
		return $value;
	}//convert hyphen values to underscore values

	public function get_current_month(){
		include_once 'db_conn.php';
		$sql_str="select month(now()) as month_now";
		$sql_qry=mysql_query($sql_str) or die(mysql_error());
		$row=mysql_fetch_array($sql_qry);
		return $row['month_now'];
	}
//include("forms.php");
	public function bool_billing_done($account_no){
		include_once 'db_conn.php';
		include_once 'cls_codes.php';
		$ledger_table=cls_misc::sanitize_hyp($account_no);
		$sql_str="select * from ldg_{$ledger_table} order by reading_date desc";
		$sql_qry=mysql_query($sql_str) or die(mysql_error("error on line 33"));
		$row=mysql_fetch_array($sql_qry);
		if (cls_misc::get_current_month()==$row['reading_date']) {
			$done='true';
		 }else {
			$done='false';
		 }
		 return $done;
	}
	
	
	public function CheckDuplication($TableName,$ColumnName1,$ColumnName2=null,$Needle1,$Needle2=null){
		include_once 'db_conn.php';
		$sql_str="select * from {$TableName} where {$ColumnName1}='{$Needle1}' and {$ColumnName2}='{$Needle2}'";
		$sql_qry=mysql_query($sql_str) or die(mysql_error());
		if(mysql_num_rows($sql_qry)>0){ //data found
			$data="1";}else{$data='0';}
		return $data;//1-duplication found,0-not found
	}
	
	//todo: [start] stage for commit to remote PS(coding status: uploaded)
	public function CheckDuplication_OpenSQL($SQL){
		include_once 'db_conn.php';
		$sql="{$SQL}";
		$e=new Exception();
		$qry=mysql_query($sql) or die(mysql_error($e->getFile()."-".$e->getLine()));
		if(mysql_numrows($qry)==0){ //duplication not found
			$data='0';
		}else{$data='1';};
		return $data;
	}
	//todo: [end] stage for commit to remote PS
	
	public function gFormatNumber($number){
		if($number==0.00){
			$data='0.00';
		}else{
			$data=number_format($number,2,".",",");    
		}
		
		return $data;
	}
	
	public function ConvertToTableName($account_no){
		$prefix="ldg_";
		$data=$prefix.cls_misc::sanitize_hyp($account_no);
		return $data;
	}
	
	//todo: [start] stage for commit to remote PS(status: uploaded)
	public function getOptBarangayNames(){
		include_once 'db_conn.php';
		$sql="select * from codes where category='Barangay' order by descr asc";
		$e=new Exception();
		$qry=mysql_query($sql) or die(mysql_error($e->getFile()."-".$e->getLine()));
		$data=array();
		while($row=mysql_fetch_array($qry)){
			$data[]=array('codes_value'=>$row['code'],'descr_value'=>$row['descr']);
		}
		//print_r($data);
		return $data;
	}
	//todo: [start] stage for commit to remote PS

	//todo: [start] stage for commit to remote PS(status: in progress)
	public function getOptCodes_OpenSQL($Category){ //return all values from table COdes filtered by category
		include_once 'db_conn.php';
		$sql="select distinct(descr),code from codes where category='{$Category}' order by id asc";
		$e=new Exception();
		$qry=mysql_query($sql) or die(mysql_error($e->getFile()."-".$e->getLine()));
		$data=array();
		while($row=mysql_fetch_array($qry)){
			$data[]=array('code_value'=>$row['code'],'descr_value'=>$row['descr']);
		}
		return $data;
	}

	public function getOptMonths(){ //return all values from table COdes filtered by category
		include_once 'db_conn.php';
		$sql="select * from codes where category='month' order by id,code asc limit 0,12";
		$e=new Exception();
		$qry=mysql_query($sql) or die(mysql_error($e->getFile()."-".$e->getLine()));
		$data=array();
		while($row=mysql_fetch_array($qry)){
			$data[]=array('code_value'=>$row['code'],'descr_value'=>$row['descr']);
		}
		return $data;
	}

	//todo: [start] stage for commit to remote PS
	
}//end class
?>